// var fs = require('fs');
//
// // 打开一个流:
// var rs = fs.createReadStream('sample.txt', 'utf-8');
//
// rs.on('data', function (chunk) {
//     console.log('DATA:');
//     console.log(chunk);
// });
//
// rs.on('end', function () {
//     console.log('END');
// });
//
// rs.on('error', function (err) {
//     console.log('ERROR: ' + err);
// });



// var fs = require('fs');
//
// var ws1 = fs.createWriteStream('output1.txt', 'utf-8');
// ws1.write('使用Stream写入文本数据...\n');
// ws1.write('END.');
// ws1.end();
//
// var ws2 = fs.createWriteStream('output2.txt');
// ws2.write(new Buffer('使用Stream写入二进制数据...\n', 'utf-8'));
// ws2.write(new Buffer('END.', 'utf-8'));
// ws2.end();



// var fs = require('fs');
//
// var rs = fs.createReadStream('sample.txt');
// var ws = fs.createWriteStream('c.txt');
//
// rs.pipe(ws);
// // rs.pipe(ws, { end: false });
// ws.write('END.');



// var path = require('path');
//
// // 解析当前目录:
// var workDir = path.resolve('.'); // '/Users/michael'
// console.log(workDir);
// // 组合完整的文件路径:当前目录+'pub'+'index.html':
// var filePath = path.join(workDir, 'pub', 'index.html');
// console.log(filePath);
// // '/Users/michael/pub/index.html'




var
    fs = require('fs'),
    url = require('url'),
    path = require('path'),
    http = require('http');

// 从命令行参数获取root目录，默认是当前目录:
var root = path.resolve(process.argv[2] || '.');

console.log('Static root dir: ' + root);

// 创建服务器:
var server = http.createServer(function (request, response) {
    // 获得URL的path，类似 '/css/bootstrap.css':
    var pathname = url.parse(request.url).pathname;
    console.log('pathname:'+pathname);
    // 获得对应的本地文件路径，类似 '/srv/www/css/bootstrap.css':
    var filepath = path.join(root, pathname);
    console.log('filepath:'+filepath);
    // 获取文件状态:
    fs.stat(filepath, function (err, stats) {
        if (!err && stats.isFile()) {
            // 没有出错并且文件存在:
            console.log('200 ' + request.url);
            // 发送200响应:
            response.writeHead(200);
            // 将文件流导向response:
            fs.createReadStream(filepath).pipe(response);
        } else {
            // 出错了或者文件不存在:
            console.log('404 ' + request.url);
            // 发送404响应:
            response.writeHead(404);
            response.end('404 Not Found');
        }
    });
});
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');